package com.jrx.anytxn.investor.bean;

import com.jrx.anytxn.investor.entity.*;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: anytxn-cf-transaction
 * @description: 联合贷还款分配保存Bean
 * @author: xiaoyu.jing
 * @date: 2020-05-25 09:44
 **/
public class InvestorPaymentSaveBean {

    private TlPaymentAllocationInvestorLog savaPaymentLog;//需要保存的还款总交易账户

    private Map<Integer, Map<String,List<AmAccountMainInvestorInfo>>> acctGroupByTenorMap = new HashMap<>();

    private List<AmAccountMainInvestorInfo> saveAcctList = new ArrayList<>();//需要保存的交易账户

    private List<AmAccountMainInvestorInfo> updateAcctList = new ArrayList<>();//需要更新的交易账户

    private List<AmInterestAccrualInvestorInfo> saveAccuAcctList = new ArrayList<>();//需要保存的累计账户

    private List<AmInterestAccrualInvestorInfo> updateAccuAcctList = new ArrayList<>();//需要更新的累计账户

    private List<TlPaymentAllocationInvestorLog> paymentLogSaveList = new ArrayList<>();//需要保存的还款子交易账户

    private List<TlGlInterfaceInvestorInfo> saveGlList = new ArrayList<>();//需要保存的gl流水

    private List<AmRepaymentPlanInvestorInfo> updatePlanList = new ArrayList<>();//需要更新的还款计划

    private List<AmRepaymentPlanInvestorInfo> savePlanList = new ArrayList<>();//需要保存的还款计划

    private List<AmDelinquencyInvestorInfo> updateDelqList = new ArrayList<>();////需要更新的延滞交易账户

    private AmLoanAccountInvestorInfo updateLoan;//需要更新的订单

    private List<AmLoanAccountInvestorInfo> updateLoanList = new ArrayList<>();//需要更新的订单

    private List<AmAccountMainInvestorInfo> allAcctList = new ArrayList<>();//需要更新和保存的所有交易账户

    private List<AmAccountMainInvestorInfo> needDealAcctList = new ArrayList<>();//所有需要处理的交易账户

    private List<BtInvestorAccountingTransferList> transferLists = new ArrayList<>();

    private List<TlFundTransferLog> transLogList = new ArrayList<>();

    private List<TlFundTransferDetail> detailList = new ArrayList<>();

    public void addAcctGroupByTypeMap(AmAccountMainInvestorInfo acct) {
        this.addAcctGroupByTypeMap(acct,acct.getAccountType());
    }

    public void addAcctGroupByTypeMapList(List<AmAccountMainInvestorInfo> acctList) {
        if(CollectionUtils.isNotEmpty(acctList)){
            for(AmAccountMainInvestorInfo acct : acctList){
                this.addAcctGroupByTypeMap(acct);
            }
        }
    }

    public Map<Integer, Map<String,List<AmAccountMainInvestorInfo>>> getAcctGroupByTenorMap() {
        return acctGroupByTenorMap;
    }

    public TlPaymentAllocationInvestorLog getSavaPaymentLog() {
        return savaPaymentLog;
    }


    public void setPaymentLogSaveList(List<TlPaymentAllocationInvestorLog> paymentLogSaveList) {
        this.paymentLogSaveList = paymentLogSaveList;
    }

    public List<BtInvestorAccountingTransferList> getTransferLists() {
        return transferLists;
    }

    public void addTransferList(BtInvestorAccountingTransferList transferList) {
        if (null != transferList) {
            this.transferLists.add(transferList);
        }
    }

    public void addTransferLists(List<BtInvestorAccountingTransferList> transferLists) {
        if (CollectionUtils.isNotEmpty(transferLists)) {
            this.transferLists.addAll(transferLists);
        }
    }

    public void setSavaPaymentLog(TlPaymentAllocationInvestorLog savaPaymentLog) {
        this.savaPaymentLog = savaPaymentLog;
    }

    public List<AmAccountMainInvestorInfo> getNeedDealAcctList() {
        if (CollectionUtils.isNotEmpty(needDealAcctList)) {
            needDealAcctList.clear();
        }

        needDealAcctList.addAll(saveAcctList);
        needDealAcctList.addAll(updateAcctList);

        return needDealAcctList;
    }

    public List<AmAccountMainInvestorInfo> getAllAcctList() {
        return allAcctList;
    }

    public void setAllAcctList(List<AmAccountMainInvestorInfo> allAcctList) {
        this.allAcctList = allAcctList;
    }

    public List<TlPaymentAllocationInvestorLog> getPaymentLogSaveList() {
        return paymentLogSaveList;
    }

    public void addPaymentLog(TlPaymentAllocationInvestorLog paymentLog) {
        if (null != paymentLog) {
            this.paymentLogSaveList.add(paymentLog);
        }
    }

    public void addPaymentLogList(List<TlPaymentAllocationInvestorLog> paymentLogList) {
        if (CollectionUtils.isNotEmpty(paymentLogList)) {
            this.paymentLogSaveList.addAll(paymentLogList);
        }
    }

    public List<AmRepaymentPlanInvestorInfo> getUpdatePlanList() {
        return updatePlanList;
    }

    public void addUpdatePlan(AmRepaymentPlanInvestorInfo updatePlan) {
        if (null != updatePlan) {
            this.updatePlanList.add(updatePlan);
        }
    }

    private void addUpdatePlanList(List<AmRepaymentPlanInvestorInfo> updatePlanList) {
        if (CollectionUtils.isNotEmpty(updatePlanList)) {
            this.updatePlanList.addAll(updatePlanList);
        }
    }

    public List<AmDelinquencyInvestorInfo> getUpdateDelqList() {
        return updateDelqList;
    }

    public void addUpdateDelq(AmDelinquencyInvestorInfo delq) {
        if (null != delq) {
            this.updateDelqList.add(delq);
        }
    }

    private void addUpdateDelqList(List<AmDelinquencyInvestorInfo> updateDelqList) {
        if (CollectionUtils.isNotEmpty(updateDelqList)) {
            this.updateDelqList.addAll(updateDelqList);
        }
    }

    public AmLoanAccountInvestorInfo getUpdateLoan() {
        return updateLoan;
    }

    public void setUpdateLoan(AmLoanAccountInvestorInfo updateLoan) {
        this.updateLoan = updateLoan;
    }

    public List<AmLoanAccountInvestorInfo> getUpdateLoanList() {
        return updateLoanList;
    }

    public List<TlGlInterfaceInvestorInfo> getSaveGlList() {
        return saveGlList;
    }

    public List<AmRepaymentPlanInvestorInfo> getSavePlanList() {
        return savePlanList;
    }

    public void addSavePlan(AmRepaymentPlanInvestorInfo plan) {
        if (null != plan) {
            this.savePlanList.add(plan);
        }
    }

    private void addSavePlanList(List<AmRepaymentPlanInvestorInfo> savePlanList) {
        if (CollectionUtils.isNotEmpty(savePlanList)) {
            this.savePlanList.addAll(savePlanList);
        }
    }

    public List<AmAccountMainInvestorInfo> getSaveAcctList() {
        return saveAcctList;
    }

    public void addSaveAcct(AmAccountMainInvestorInfo saveAcct) {
        if (null != saveAcct) {
            this.saveAcctList.add(saveAcct);
        }
    }

    public void setSaveAcctList(List<AmAccountMainInvestorInfo> saveAcctList) {
        this.saveAcctList = saveAcctList;
    }

    public void addSaveAcctList(List<AmAccountMainInvestorInfo> saveAcctList) {
        if (CollectionUtils.isNotEmpty(saveAcctList)) {
            this.saveAcctList.addAll(saveAcctList);
        }
    }

    public List<AmAccountMainInvestorInfo> getUpdateAcctList() {
        return updateAcctList;
    }

    public void setUpdateAcctList(List<AmAccountMainInvestorInfo> updateAcctList) {
        this.updateAcctList = updateAcctList;
    }

    public void addUpdateAcct(AmAccountMainInvestorInfo updateAcct) {
        if (null != updateAcctList) {
            this.updateAcctList.add(updateAcct);
        }
    }

    private void addUpdateAcctList(List<AmAccountMainInvestorInfo> updateAcctList) {
        if (CollectionUtils.isNotEmpty(updateAcctList)) {
            this.updateAcctList.addAll(updateAcctList);
        }
    }


    public List<AmInterestAccrualInvestorInfo> getSaveAccuAcctList() {
        return saveAccuAcctList;
    }

    private void addUpdateAccuAcctList(List<AmInterestAccrualInvestorInfo> updateAccuAcctList) {
        if (CollectionUtils.isNotEmpty(updateAccuAcctList)) {
            this.updateAccuAcctList.addAll(updateAccuAcctList);
        }
    }

    public void addSaveAccuAcctList(List<AmInterestAccrualInvestorInfo> saveAccuAcctList) {
        if (CollectionUtils.isNotEmpty(saveAccuAcctList)) {
            this.saveAccuAcctList.addAll(saveAccuAcctList);
        }
    }

    public List<AmInterestAccrualInvestorInfo> getUpdateAccuAcctList() {
        return updateAccuAcctList;
    }

    public void addUpdateAccuAcct(AmInterestAccrualInvestorInfo amInterestAccrualInfo) {
        if (null != amInterestAccrualInfo) {
            this.updateAccuAcctList.add(amInterestAccrualInfo);
        }
    }


    public void addSaveGlList(List<TlGlInterfaceInvestorInfo> saveGlList) {
        if (CollectionUtils.isNotEmpty(saveGlList)) {
            this.saveGlList.addAll(saveGlList);
        }
    }

    public void addSaveGl(TlGlInterfaceInvestorInfo gl) {
        if (null != gl) {
            this.saveGlList.add(gl);
        }
    }

    public void addUpdateLoan(AmLoanAccountInvestorInfo updateLoan) {
        if (null != updateLoan) {
            this.updateLoanList.add(updateLoan);
        }
    }

    public void setSaveAccuAcctList(List<AmInterestAccrualInvestorInfo> saveAccuAcctList) {
        this.saveAccuAcctList = saveAccuAcctList;
    }

    public void setUpdateAccuAcctList(List<AmInterestAccrualInvestorInfo> updateAccuAcctList) {
        this.updateAccuAcctList = updateAccuAcctList;
    }


    public void setSaveGlList(List<TlGlInterfaceInvestorInfo> saveGlList) {
        this.saveGlList = saveGlList;
    }

    public void addTransLog(TlFundTransferLog log){
        this.transLogList.add(log);
    }

    public void addDetl(TlFundTransferDetail detl){
        this.detailList.add(detl);
    }

    public void addTransLogs(List<TlFundTransferLog> logList){
        if(CollectionUtils.isNotEmpty(logList)) {
            this.transLogList.addAll(logList);
        }
    }

    public void addDetls(List<TlFundTransferDetail> detlList){
        if(CollectionUtils.isNotEmpty(detlList)) {
            this.detailList.addAll(detlList);
        }
    }

    public List<TlFundTransferLog> getTransLogList() {
        return transLogList;
    }

    public List<TlFundTransferDetail> getDetailList() {
        return detailList;
    }


    /**
     * saveBean合并方法
     *
     * @param orgiSaveBean
     * @return
     */
    public InvestorPaymentSaveBean add(InvestorPaymentSaveBean orgiSaveBean) {
        this.setSavaPaymentLog(orgiSaveBean.getSavaPaymentLog());
        this.addPaymentLogList(orgiSaveBean.getPaymentLogSaveList());
        this.addSaveAccuAcctList(orgiSaveBean.getSaveAccuAcctList());
        this.addUpdateAccuAcctList(orgiSaveBean.getUpdateAccuAcctList());
        this.addSaveAcctList(orgiSaveBean.getSaveAcctList());
        this.addUpdateAcctList(orgiSaveBean.getUpdateAcctList());
        this.addSaveGlList(orgiSaveBean.getSaveGlList());
        this.addSavePlanList(orgiSaveBean.getSavePlanList());
        this.addUpdatePlanList(orgiSaveBean.getUpdatePlanList());
        this.addUpdateDelqList(orgiSaveBean.getUpdateDelqList());
        this.addUpdateLoan(orgiSaveBean.getUpdateLoan());
        this.addTransferLists(orgiSaveBean.getTransferLists());
        this.addTransLogs(orgiSaveBean.getTransLogList());
        this.addDetls(orgiSaveBean.getDetailList());
        return this;
    }

    public void addAcctGroupByTypeMap(AmAccountMainInvestorInfo acct, String accountType) {
        if(null == acct){
            return;
        }

        Map<String,List<AmAccountMainInvestorInfo>> acctGroupByTypeMap = acctGroupByTenorMap.get(acct.getBillingTenor());
        if(null != acctGroupByTypeMap){
            List<AmAccountMainInvestorInfo> acctList = acctGroupByTypeMap.get(accountType);
            if(CollectionUtils.isNotEmpty(acctList)){
                acctList.add(acct);
            }else{
                acctList = new ArrayList<>();
                acctList.add(acct);
                acctGroupByTypeMap.put(accountType,acctList);
            }
        }else{
            List<AmAccountMainInvestorInfo> acctList = new ArrayList<>();
            acctList.add(acct);
            acctGroupByTypeMap = new HashMap<>();
            acctGroupByTypeMap.put(accountType,acctList);
            acctGroupByTenorMap.put(acct.getBillingTenor(),acctGroupByTypeMap);
        }
    }
}
